#!/bin/sh

# Find hosts which are still using a bond 'slave' for their management interface
# and try to move them onto the bond 'master' (ie activate the bond). Will
# only work if there are no active guest VIFs.

IFS=","; for bond in $(xe bond-list params=uuid --minimal); do
  master=$(xe bond-list uuid=$bond params=master --minimal)
  IFS=";"; for slave in $(xe bond-list uuid=$bond params=slaves --minimal); do
     IFS=","; for pif in $(xe pif-list uuid=$slave management=true --minimal); do
       echo Found a slave management PIF: $pif
       mode=$(xe pif-list uuid=$pif params=IP-configuration-mode --minimal)
       ip=$(xe pif-list uuid=$pif params=IP --minimal)
       netmask=$(xe pif-list uuid=$pif params=netmask --minimal)
       gateway=$(xe pif-list uuid=$pif params=gateway --minimal)
       dns=$(xe pif-list uuid=$pif params=DNS --minimal)
       echo Setting IP configuration on bond PIF: $master
       xe pif-reconfigure-ip uuid=$master mode=$mode IP=$ip netmask=$netmask gateway=$gateway DNS=$dns
       host=$(xe pif-list uuid=$master params=host-uuid --minimal)
       echo Moving management interface of host $host to bond
       xe host-management-reconfigure pif-uuid=$master
     done
  done
done
